package com.vf.admin.utils.Base64Image;

import com.vf.admin.common.Constants;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * 多图上传，删除旧图片
 */
public class ImgListDel {

    /**
     * 删除老html中新html已经不用的图片,本方法用于处理的html中的图片都是在本地的图片
     */
    public static void delOldImgList(String oldHtml, String newHtml,String picBasePsth) {
        if(oldHtml==null){
            oldHtml = "";
        }
        if(newHtml==null){
            newHtml = "";
        }
        String[] oldImgList = oldHtml.split(",");
        String[] newImgList = newHtml.split(",");
        Map<String, String> newMap = new HashMap<String, String>();

        for (String newimg : newImgList) {
            newMap.put(newimg, newimg);
        }
        for (String oldimg : oldImgList) {
            Object oldimgobj = newMap.get(oldimg);
            if (oldimgobj == null) {
                delPic(oldimg);
            }
        }
    }

    public static void delPic(String imgPath) {
        String picName = Constants.PATH_OS_BASE + imgPath;
        File file = new File(picName);
        if (file.exists()) {
            file.delete();
        }
    }

}
